package com.heima.admin.controller.v1;

import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.admin.service.AdChannelService;
import io.swagger.annotations.ApiOperation;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/v1/channel")
@MapperScan("com.heima.admin.mapper")
public class AdminController {

    @Autowired
    AdChannelService service;

    /**
     * 频道查询功能
     */
    @PostMapping("/list")
    public ResponseResult findAdchannel(@RequestBody ChannelDTO channelDTO){
        return service.findAdchannel(channelDTO);
    }

    @GetMapping("/channels")
    public ResponseResult findAll() {
        List<AdChannel> list = service.list();
        return ResponseResult.okResult(list);
    }
    /**
     * 新增频道
     * @param channel
     * @return
     */
    @PostMapping("/save")
    public ResponseResult save(@RequestBody AdChannel channel){
        return service.insert(channel);
    }

    /**
     * 频道修改
     * @param channel
     * @return
     */
    @PostMapping("update")
    public ResponseResult update(@RequestBody AdChannel channel){
        return service.updateForId(channel);
    }

    /**
     * 根据id删除频道
     * @param id
     * @return
     */
    @GetMapping("del/{id}")
    public ResponseResult delete(@PathVariable Integer id){
        return service.delete(id);
    }

    @GetMapping("/one/{id}")
    public ResponseResult findOne(@PathVariable Integer id) {
        return ResponseResult.okResult(service.getById(id));
    }

}
